package com.ishow.noah.modules.main

import android.os.Bundle
import androidx.core.widget.addTextChangedListener
import com.ishow.common.adapter.BindAdapter
import com.ishow.common.extensions.toJSON
import com.ishow.common.utils.router.AppRouter
import com.ishow.common.widget.recyclerview.itemdecoration.ColorDecoration
import com.ishow.noah.BR
import com.ishow.noah.R
import com.ishow.noah.databinding.AMainBinding
import com.ishow.noah.entries.App
import com.ishow.noah.modules.base.mvvm.view.AppBindActivity
import kotlinx.android.synthetic.main.a_main.*

/**
 * Created by yuhaiyang on 2020-01-02.
 */
class MainActivity : AppBindActivity<AMainBinding, MainViewModel>() {
    private var vm: MainViewModel? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindContentView(R.layout.a_main)
    }

    override fun initViews() {
        super.initViews()

        val adapter = BindAdapter<App>()
        adapter.addLayout(BR.item, R.layout.item_app_list)
        adapter.setOnItemClickListener { viewDetail(adapter.getItem(it)) }
        list.addItemDecoration(ColorDecoration())
        list.adapter = adapter

        search.addTextChangedListener { text -> vm?.search(text.toString()) }
    }

    override fun initViewModel(vm: MainViewModel) {
        super.initViewModel(vm)
        this.vm = vm
    }


    private fun viewDetail(app: App) {
        AppRouter.with(this)
            .target(AppDetailActivity::class.java)
            .addParam(App.Key.DETAIL, app.toJSON())
            .start()
    }
}